//
//  DYZSalesTerritoryController.m
//  DYZCars
//
//  Created by 白白龙腾 on 16/8/19.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZSalesTerritoryController.h"
#import "DYZCustomTerritoryController.h"
static NSString *cellid = @"cellid";
@interface DYZSalesTerritoryController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic , strong)NSDictionary *citysDict;
@property (nonatomic , strong)NSArray *citysKey;
@end

@implementation DYZSalesTerritoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    UIButton *leftBtn = [[UIButton alloc]init];
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn sizeToFit];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = item;
    [leftBtn addTarget:self action:@selector(aButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.title = @"销售区域";
    
    // Do any additional setup after loading the view.
    self.citysDict = [self loadCityListData];
    self.citysKey = [[self.citysDict allKeys]sortedArrayUsingSelector:@selector(compare:)];
}

//初始化UI
-(void)setupUI{
    
    self.view.backgroundColor = [UIColor redColor];
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:bottomView];
    
    UITableView *tabView = [[UITableView alloc]init];
    [self.view addSubview:tabView];
    
    tabView.delegate = self;
    tabView.dataSource = self;
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"自定义区域";
    [label sizeToFit];
    label.textColor = [UIColor whiteColor];
    [bottomView addSubview:label];
    
    UIButton *button = [[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:@"u51"] forState:UIControlStateNormal];
    [button sizeToFit];
    [bottomView addSubview:button];
    
    //自动布局
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.offset(64);
    }];
    [tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(bottomView.mas_top);
        make.top.offset(64);
    }];
    //label和button自动布局
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomView);
        make.left.equalTo(bottomView).offset(16);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomView);
        make.right.equalTo(bottomView).offset(16);
    }];
    
    [tabView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellid];
    
    //创建手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    //添加手势
    [bottomView addGestureRecognizer:tapGesture];
}

- (void)tapGesture:(UIView *)sender{
    DYZCustomTerritoryController *ctc = [[DYZCustomTerritoryController alloc]init];
    [self.navigationController pushViewController:ctc animated:YES];
}

//点击aButton
- (void)aButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.citysKey.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = self.citysKey[section];
    NSArray *city = self.citysDict[key];
    return city.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
    NSArray *key = self.citysKey[indexPath.section];
    NSArray *cityse= self.citysDict[key];
    NSString *cityName = cityse[indexPath.row];
    cell.textLabel.text = cityName;
    return cell;
}

//组头内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.citysKey[section];
}

//设置组尾的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

//右侧索引
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.citysKey;
}

//点击效果
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1 ] animated:YES];
    
}
//解析Plist
- (NSDictionary *)loadCityListData{
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"cityList.plist" withExtension:nil];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:url];
    
    return dict;
}@end
